package collections;

import java.util.Deque;
import java.util.LinkedList;

public class DequeTest3 {

    public static void main(String[] args) {

        Deque<String> deque = new LinkedList<>();

        deque.offerLast("龚怡群");
        deque.offer("何艺庆");
        deque.offerLast("乐闽庭");
        System.out.println( deque.toString() );

        System.out.println( deque.pollLast() + " 出列 " );//出队：先进先出( First In , First Out )
        System.out.println( deque.toString() );
        System.out.println( deque.pollLast() + " 出列 " );
        System.out.println( deque.toString() );
        System.out.println( deque.pollLast() + " 出列 " );
        System.out.println( deque.toString() );

        System.out.println( "~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~" );

        deque.offerFirst( "何思豪" );
        deque.offer( "周义富" );
        deque.offerFirst( "何伟华" );
        System.out.println( deque.toString() );

        System.out.println( deque.pollLast() + " 出列 " );// 移除 队列头部 元素
        System.out.println( deque.toString() );
        System.out.println( deque.pollLast() + " 出列 " );
        System.out.println( deque.toString() );
        System.out.println( deque.pollLast() + " 出列 " );
        System.out.println( deque.toString() );

        System.out.println( "~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~" );

        // 以 "字符串形式" 右侧为【栈顶】，以 "字符串形式" 左侧为【栈底】
        deque.offerLast( "刘彻" ); // 向【栈顶】压入元素
        deque.offerLast( "刘启" ); // 向【栈顶】压入元素
        deque.offerLast( "刘恒" ); // 向【栈顶】压入元素
        System.out.println( deque.toString() );

        System.out.println( deque.peekLast() ) ; // 检查栈顶元素
        System.out.println( deque );

        System.out.println( deque.pollLast() ) ; // 弹出栈顶元素
        System.out.println( deque );

        System.out.println( deque.pollLast() ) ; // 弹出栈顶元素
        System.out.println( deque );

        System.out.println( deque.pollLast() ) ; // 弹出栈顶元素
        System.out.println( deque );

        System.out.println( "~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~" );

        // 以 "字符串形式" 左侧为【栈顶】，以 "字符串形式" 右侧为【栈底】
        deque.offerFirst( "乾隆" );
        deque.offerFirst( "雍正" );
        deque.offerFirst( "康熙" ); // 向【栈顶】压入元素

        System.out.println( deque );

        System.out.println( deque.peekFirst() ) ; // 检查栈顶元素
        System.out.println( deque );

        System.out.println( deque.pollFirst() ) ; // 弹出栈顶元素
        System.out.println( deque );

        System.out.println( deque.pollFirst() ) ; // 弹出栈顶元素
        System.out.println( deque );

        System.out.println( deque.pollFirst() ) ; // 弹出栈顶元素
        System.out.println( deque );

        System.out.println( "~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~" );

        // java.util.Deque 接口中直接定义了 "栈" 操作相关的方法:  push( E item ) / peek() / pop()
        deque.push( "孙尚香" );//将元素压入栈顶
        deque.push( "马可波罗" );
        deque.push( "廉颇" );
        deque.push( "李白" );

        System.out.println( deque.toString() );

        System.out.println( deque.peek() );//检查栈顶元素
        System.out.println( deque );

        System.out.println( deque.pop() );//弹出栈顶元素
        System.out.println( deque );

        System.out.println( deque.pop() );//弹出栈顶元素
        System.out.println( deque );

        System.out.println( deque.pop() );//弹出栈顶元素
        System.out.println( deque );

        System.out.println( deque.pop() );//弹出栈顶元素
        System.out.println( deque );

    }
}
